/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal_fastdds/listener_impl/DomainParticipantListenerImpl.hpp"
#include "dal_fastdds/entity_impl/DalTopicImpl.hpp"
#include "dal_fastdds/entity_impl/DalDataWriterImpl.hpp"
#include "dal_fastdds/entity_impl/DalDataReaderImpl.hpp"
#include "dal_fastdds/entity_impl/DalSubscriberImpl.hpp"
#include "dal_fastdds/utils/log.hpp"
#include "dal_fastdds/utils/EntityMappingManager.hpp"

DomainParticipantListenerImpl::DomainParticipantListenerImpl(
    dal_domain_participant_listener_callbacks_t callbacks)
{
    callbacks_ = callbacks;
    participant_monitor_callback_ = nullptr;
}

DomainParticipantListenerImpl::~DomainParticipantListenerImpl() {}

void DomainParticipantListenerImpl::on_inconsistent_topic(Topic* topic,
                                                          InconsistentTopicStatus status)
{
    if (callbacks_.on_inconsistent_topic == nullptr)
        return;

    dal_topic_t* dal_topic = EntityMappingGetTopic(topic);
    if (!dal_topic) {
        return;
    }

    dal_inconsistent_topic_status_t dal_status;
    StatusConverter::GetInstance()->ConvertInconsistentTopicStatusFromFastddsToDal(&status,
                                                                                   &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_inconsistent_topic)(dal_topic, &dal_status, data);
}

void DomainParticipantListenerImpl::on_liveliness_lost(DataWriter* writer,
                                                       const LivelinessLostStatus& status)
{
    if (callbacks_.on_liveliness_lost == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_liveliness_lost_status_t dal_status;
    StatusConverter::GetInstance()->ConvertLivelinessLostStatusFromFastddsToDal(&status,
                                                                                &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_liveliness_lost)(dal_writer, &dal_status, data);
}

void DomainParticipantListenerImpl::on_offered_deadline_missed(
    DataWriter* writer, const OfferedDeadlineMissedStatus& status)
{
    if (callbacks_.on_offered_deadline_missed == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_offered_deadline_missed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertOfferedDeadlineMissedStatusFromFastddsToDal(&status,
                                                                                       &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_offered_deadline_missed)(dal_writer, &dal_status, data);
}

void DomainParticipantListenerImpl::on_offered_incompatible_qos(
    DataWriter* writer, const OfferedIncompatibleQosStatus& status)
{
    if (callbacks_.on_offered_incompatible_qos == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_offered_incompatible_qos_status_t dal_status;
    StatusConverter::GetInstance()->ConvertOfferedIncompatibleQosStatusFromFastddsToDal(
        &status, &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_offered_incompatible_qos)(dal_writer, &dal_status, data);
}

void DomainParticipantListenerImpl::on_data_on_readers(Subscriber* sub)
{
    if (callbacks_.on_data_on_readers == nullptr)
        return;

    dal_subscriber_t* dal_sub = EntityMappingGetSubscriber(sub);
    if (!dal_sub) {
        return;
    }

    void* data = callbacks_.data_;
    (callbacks_.on_data_on_readers)(dal_sub, data);
}

void DomainParticipantListenerImpl::on_sample_lost(DataReader* reader,
                                                   const SampleLostStatus& status)
{
    if (callbacks_.on_sample_lost == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_sample_lost_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSampleLostStatusFromFastddsToDal(&status, &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_sample_lost)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_data_available(DataReader* reader)
{
    if (callbacks_.on_data_available == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    void* data = callbacks_.data_;
    (callbacks_.on_data_available)(dal_reader, data);
}

void DomainParticipantListenerImpl::on_sample_rejected(DataReader* reader,
                                                       const SampleRejectedStatus& status)
{
    if (callbacks_.on_sample_rejected == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_sample_rejected_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSampleRejectedStatusFromFastddsToDal(&status,
                                                                                &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_sample_rejected)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_liveliness_changed(DataReader* reader,
                                                          const LivelinessChangedStatus& status)
{
    if (callbacks_.on_liveliness_changed == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_liveliness_changed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertLivelinessChangedStatusFromFastddsToDal(&status,
                                                                                   &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_liveliness_changed)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_requested_deadline_missed(DataReader* reader,
                                                                 const DeadlineMissedStatus& status)
{
    if (callbacks_.on_requested_deadline_missed == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_requested_deadline_missed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertRequestedDeadlineMissedStatusFromFastddsToDal(
        &status, &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_requested_deadline_missed)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_requested_incompatible_qos(
    DataReader* reader, const RequestedIncompatibleQosStatus& status)
{
    if (callbacks_.on_requested_incompatible_qos == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_requested_incompatible_qos_status_t dal_status;
    StatusConverter::GetInstance()->ConvertRequestedIncompatibleQosStatusFromFastddsToDal(
        &status, &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_requested_incompatible_qos)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::on_publication_matched(DataWriter* writer,
                                                           const PublicationMatchedStatus& info)
{
    if (callbacks_.on_publication_matched == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_publication_matched_status_t dal_status;
    StatusConverter::GetInstance()->ConvertPublicationMatchedStatusFromFastddsToDal(&info,
                                                                                    &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_publication_matched)(dal_writer, &dal_status, data);
}

void DomainParticipantListenerImpl::on_subscription_matched(DataReader* reader,
                                                            const SubscriptionMatchedStatus& info)
{
    if (callbacks_.on_subscription_matched == nullptr)
        return;

    dal_datareader_t* dal_reader = EntityMappingGetDataReader(reader);
    if (!dal_reader) {
        return;
    }

    dal_subscription_matched_status_t dal_status;
    StatusConverter::GetInstance()->ConvertSubscriptionMatchedStatusFromFastddsToDal(&info,
                                                                                     &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_subscription_matched)(dal_reader, &dal_status, data);
}

void DomainParticipantListenerImpl::set_participant_monitor_callback(
    dal_participant_monitor_callback callback)
{
    participant_monitor_callback_ = callback;
}

void DomainParticipantListenerImpl::set_participant_monitor_callback_data(void* data)
{
    participant_monitor_callback_data_ = data;
}

void DomainParticipantListenerImpl::on_participant_discovery(DomainParticipant* participant,
                                                             ParticipantDiscoveryInfo&& info)
{
    if (!participant_monitor_callback_)
        return;

    dal_participant_monitor_info_t monitor_info;
    memset(&monitor_info, 0, sizeof(dal_participant_monitor_info_t));

    auto status = info.status;
    switch (status) {
        case ParticipantDiscoveryInfo::DISCOVERED_PARTICIPANT:
            monitor_info.status = DAL_PARTICIPANT_ONLINE;
            break;
        case ParticipantDiscoveryInfo::REMOVED_PARTICIPANT:
            monitor_info.status = DAL_PARTICIPANT_OFFLINE_NORMALLY;
            break;
        case ParticipantDiscoveryInfo::DROPPED_PARTICIPANT:
            monitor_info.status = DAL_PARTICIPANT_OFFLINE_ABNORMALLY;
            break;
        default:
            break;
    }
    GuidConverter::GetInstance()->ConvertGuidFromFastddsToDal(&(info.info.m_guid),
                                                              &(monitor_info.guid));

    SET_STRING(&(monitor_info.participant_name),
               info.info.m_participantName.c_str(),
               info.info.m_participantName.size());
    participant_monitor_callback_(&monitor_info, participant_monitor_callback_data_);
    RESET_STRING(&(monitor_info.participant_name));
}